import 'package:favourcate/core/extension/int_fit_ext.dart';
import 'package:favourcate/core/model/category_model.dart';
import 'package:favourcate/core/services/json_parse.dart';
import 'package:favourcate/ui/pages/home/home_category_item.dart';
import 'package:flutter/material.dart';

class GLHomeContent1 extends StatefulWidget {
  const GLHomeContent1({ Key? key }) : super(key: key);

  @override
  _GLHomeContent1State createState() => _GLHomeContent1State();
}

class _GLHomeContent1State extends State<GLHomeContent1> {

  List<GLCategoryModel> _categories = [];


  @override
  void initState() {
    super.initState();
    JsonParse.getCategoryData().then((res) {
      // print(res);
      setState(() {
        _categories = res;
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return  GridView.builder(
      padding: EdgeInsets.all(20.px),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 20.px,
        mainAxisSpacing: 20.px,
        childAspectRatio: 1.5,
      ),
      itemCount: _categories.length,
      itemBuilder: (BuildContext context, int index) {
        return GLHomeCategoryItem(_categories[index]);
      },
    );
  }
}


/// * FutureBuilder 优化
/// 

class GLHomeContent extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<GLCategoryModel>>(
      future: JsonParse.getCategoryData(),
      // initialData: InitialData,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(),
          );
        }
        final categories = snapshot.data;
        return  GridView.builder(
          padding: EdgeInsets.all(20.px),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 20.px,
            mainAxisSpacing: 20.px,
            childAspectRatio: 1.5,
          ),
          itemCount: categories.length,
          itemBuilder: (BuildContext context, int index) {
            return GLHomeCategoryItem(categories[index]);
          },
        );
      },
    );
  }
}
